//
//  AppDelegate.m
//  17-SandBox
//
//  Created by dllo on 16/8/5.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "AppDelegate.h"
#import "Book.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 1.沙盒(SandBox)
        // Documents: 文件文档, 用户数据存储
        // Library: 资源, 一般用来存放, 程序员要存储的一些数据
            // Caches(Library目录下文件): 缓存文件
            // Preferences((Library目录下文件): 用户信息, 用户设置, NSUserDefaults
        // tmp:临时目录, 下载的临时文件一般放在这里
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLogin"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    // 2.获取沙盒路径
    // 快捷获取到对应目录的 C 函数
    // 根目录, 家目录
    NSLog(@"Home ---- %@", NSHomeDirectory());
    // 临时目录 tmp
    NSLog(@"Temporary ------%@", NSTemporaryDirectory());
   /**
    参数1: 搜索哪个文件夹路径
    常用:(1) NSDocumentDirectory
        (2) NSLibraryDirectory
        (3) NSCachesDirectory
    参数2: 在用户作用域下搜索
    常用: NSUserDomainMask
    参数3: YES or NO , YES代表绝对路径(基本上都用这个), NO代表相对路径
    */
    
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    
    NSLog(@"%@", pathArray.firstObject);
    // NSBundle 是 .app文件包
    NSLog(@"%@", [NSBundle mainBundle]);
    
    // 1> 简单的文件读写  Input(写入)  Output(写出)
    NSString *helloString = @"Hello, I/O file";
    // 拼接路径, 生成文件类型后缀
    // 一般拼接路径时, 使用 NSString *writePath = [[pathArray firstObject] stringByAppendingString:@"/hello.txt"];
     NSString *writePath = [[pathArray firstObject] stringByAppendingPathComponent:@"hello.text"];
    NSError *error = nil;
    BOOL isSuccess = [helloString writeToFile:writePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"%d", isSuccess);
    if (error) {
        NSLog(@"存储失败");
    } else {
        NSLog(@"恭喜存储成功");
    }
    // 2> 读取路径对应得文字
    NSError *readError = nil;
    NSString *readString = [NSString stringWithContentsOfFile:writePath encoding:NSUTF8StringEncoding error:&readError];
    NSLog(@"%@", readString);
    
    // 3> 将数组写入本地文件
    NSArray *array = @[@"Tom", @"Daming", @"mary"];
    NSString *arratPath = [[pathArray firstObject] stringByAppendingPathComponent:@"name.plist"];
    BOOL isArrayWriteSuccess = [array writeToFile:arratPath atomically:YES];
    if (isArrayWriteSuccess) {
        NSLog(@"写入数组成功");
    } else {
        NSLog(@"写入数组失败");
    }
    
    // 4> 将数组读入
    NSArray *nameArray = [NSArray arrayWithContentsOfFile:arratPath];
    NSLog(@"%@", nameArray);
    
    // 5> 将字典存入
    NSDictionary *dic = @{@"name":@"Tom",
                          @"age":@18,
                          @"sex":@"male"};
    NSString *dicPath = [[pathArray firstObject] stringByAppendingPathComponent:@"dic.plist"];
    BOOL isDicWriteSuccess = [dic writeToFile:dicPath atomically:YES];
    if (isDicWriteSuccess) {
        NSLog(@"写入字典成功");
    } else {
        NSLog(@"写入字典失败");
    }
    // 6> 将字典读出
    NSDictionary *outDic = [NSDictionary dictionaryWithContentsOfFile:dicPath];
    NSLog(@"%@", outDic);
    
    // 7> 将Data类型写入本地
    UIImage *image = [UIImage imageNamed:@"1.jpg"];
    // 将jpg类型的图片转化成NSData二进制类型
    NSData *imageData = UIImageJPEGRepresentation(image, 0.1);
    // 将png类型的图片转化成NSData二进制类型
    // UIImagePNGRepresentation(<#UIImage * _Nonnull image#>)
    NSString *dataPath = [[pathArray firstObject] stringByAppendingPathComponent:@"imageData.a"];
    BOOL isDataSuccess = [imageData writeToFile:dataPath atomically:YES];
    if (isDataSuccess) {
        NSLog(@"写入data成功");
    } else {
        NSLog(@"写入data失败");
    }
    // 8>读取data类型
    NSData *imageNewData = [NSData dataWithContentsOfFile:dataPath];
    // 将data类型转换为图片类型
    UIImage *imageWithData = [UIImage imageWithData:imageNewData];
    
    
    // 2.复杂对象文件读写, 自定义恶性
    // 归档/反归档, 序列化/反序列化
    // 1> 归档, 将对象存储到本地
    Book *book = [Book new];
    book.bookName = @"iOS从入门到放弃";
    book.bookType = @"教育类";
    book.bookPrice = @"0.1";
    book.bookAuthor = @"Tom";
    book.bookAddress = @"xxxx出版社";
    NSString *bookPath = [[pathArray firstObject] stringByAppendingPathComponent:@"book.plist"];
    BOOL isBookSuccess = [NSKeyedArchiver archiveRootObject:book toFile:bookPath];
    if (isBookSuccess) {
        NSLog(@"写入图书成功");
    } else {
        NSLog(@"写入图书失败");
    }
    // 2> 反归档
    Book *book1 = [NSKeyedUnarchiver unarchiveObjectWithFile:bookPath];
    NSLog(@"%@", book1.bookAddress);
    return YES;
    
    // 如果对象想要实现归档和反归档
    // 1.对🐘(象)对应的类需要签订<NSCoding>
    // 2.实现协议方法
        // 1> initWithCoder 反归档用
        // 2> encodeWithCoder 归档用
    // 3. 归档时, 使用 NSKeyedArchiver
    // 4. 反归档时, 使用 NSKeyedUnarchiver
}

@end
